package org.ricks.condition2.controller;

import org.ricks.condition2.Condition;
import org.ricks.condition2.ConditionManager;
import org.ricks.condition2.event.PlayerLevelUpEvent;
import org.ricks.condition2.test.ConditionConfig;
import org.ricks.condition2.test.ConditionParser;
import org.ricks.ioc.anno.Bean;
import org.ricks.ioc.event.anno.EventReceiver;

import java.util.List;

@Bean
public class PlayerController {

    @EventReceiver
    public void levelUp (PlayerLevelUpEvent playerLevelUpEvent) {
        ConditionManager.ME().onEvent(playerLevelUpEvent);
    }


    // 玩家登录时加载所有进行中的条件
    public void onPlayerLogin(String playerId) {
        List<ConditionConfig> activeConditions = loadActiveConditions(playerId);
        activeConditions.forEach(config -> {
            Condition condition = ConditionParser.parseCondition(playerId, config);
            ConditionManager.ME().register(condition);
        });
    }

    private List<ConditionConfig> loadActiveConditions(String playerId) {
        return List.of();
    }

    // 玩家离线时清理条件
    public void onPlayerLogout(String playerId) {
        ConditionManager.ME().cleanPlayerConditions(playerId);
    }


    public void reloadConditions(String playerId) {
        ConditionManager.ME().cleanPlayerConditions(playerId);
        loadAllConfigs().forEach(config -> registerToAllPlayers(config));
    }

    private void registerToAllPlayers(ConditionConfig config) {

    }

    private List<ConditionConfig> loadAllConfigs() {
        return List.of();
    }
}
